#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>

char* toans(long long n, int ne) {
    static char result[100];
    int index = 0;
    if (n == 0) {
        result[index++] = '0';
        result[index] = '\0';
        return result;
    }
    while (n) {
        int tm = n % ne;
        if (tm <= 9)
            result[index++] = '0' + tm;
        else
            result[index++] = 'A' + tm - 10;
        n /= ne;
    }
    result[index] = '\0';
    for (int i = 0; i < index / 2; i++) {
        char temp = result[i];
        result[i] = result[index - i - 1];
        result[index - i - 1] = temp;
    }
    return result;
}

long long toten(char* str, int ne) {
    long long ans = 0, arg = 1;
    int len = strlen(str);
    for (int i = len - 1; i >= 0; i--) {
        if (str[i] <= '9')
            ans += (str[i] - '0') * arg;
        else
            ans += (str[i] - 'A' + 10) * arg;
        arg *= ne;
    }
    return ans;
}

long long getop(long long a, long long b, char* op) {
    if (strcmp(op, "ADD") == 0)
        return a + b;
    else if (strcmp(op, "SUB") == 0)
        return a - b;
    else if (strcmp(op, "MUL") == 0)
        return a * b;
    else if (strcmp(op, "DIV") == 0)
        return a / b;
    else
        return a % b;
}

int main() {
    int n;
    scanf("%d", &n);
    long long ans = 0;
    int ne = 10;
    char str[100], op[100];
    while (n--) {
        scanf("%s", str);
        if (strcmp(str, "CLEAR") == 0) {
            memset(str, 0, sizeof(str));
            memset(op, 0, sizeof(op));
        }
        else if (strcmp(str, "EQUAL") == 0) {
            memset(op, 0, sizeof(op));
            printf("%s\n", toans(ans, ne));
        }
        else if (strcmp(str, "CHANGE") == 0) {
            scanf("%d", &ne);
        }
        else if (strcmp(str, "NUM") == 0 && op[0] == '\0') {
            scanf("%s", str);
            ans = toten(str, ne);
        }
        else if (strcmp(str, "NUM") == 0 && op[0] != '\0') {
            scanf("%s", str);
            ans = getop(ans, toten(str, ne), op);
        }
        else {
            strcpy(op, str);
        }
    }
    return 0;
}
